'''
Created on Mar 23, 2013

@author: Petko
'''

from uuid import UUID


class AttentionDelta():
    
    def __repr__(self):
        return "<AttentionDelta id=%s p=%s n=%s>" % (str(UUID(bytes=self.op_id)), str(self.positive_attention_delta), str(self.negative_attention_delta))
    
    def toPrimitiveDict(self):
        return {'opinion_id': str(UUID(bytes=self.op_id)),
                'positive_attention_delta': self.positive_attention_delta,
                'negative_attention_delta': self.negative_attention_delta}
        
    def __init__(self, op_id = None, positiveDelta = 0, negativeDelta = 0):
        self.op_id = op_id
        self.positive_attention_delta = positiveDelta
        self.negative_attention_delta = negativeDelta
        
        
class Deltas():
    def __init__(self):
        self._deltas = {}
        
    def change_delta(self, op_id, positiveDelta, negativeDelta):
        opUUID = UUID(bytes = op_id)
        delta = self._deltas.get(opUUID, None)
        if delta:
            assert isinstance(delta, AttentionDelta)
            delta.positive_attention_delta += positiveDelta
            delta.negative_attention_delta += negativeDelta
        else:
            self._deltas[opUUID] = AttentionDelta(op_id, positiveDelta, negativeDelta)
    
    def getDelta(self, op_id):
        opUUID = UUID(bytes = op_id)
        return self._deltas.get(opUUID, None)
    
    def getAllDeltas(self):
        return self._deltas.itervalues()
    
class ParentStat():
    def __init__(self):
        self.content_subops_delta = 0 
        self.positive_subops_delta = 0
        self.neutral_subops_delta = 0
        self.negative_subops_delta = 0
    
    def add_stats(self, content_subops_delta, positive_subops_delta, neutral_subops_delta, negative_subops_delta):
        if content_subops_delta:
            self.content_subops_delta += content_subops_delta
        
        if positive_subops_delta:
            self.positive_subops_delta += positive_subops_delta
        
        if negative_subops_delta:
            self.negative_subops_delta += negative_subops_delta
            
        if neutral_subops_delta:
            self.neutral_subops_delta += neutral_subops_delta
    
    